package com.lock.immersive.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lock.immersive.annotation.AuthCheck;
import com.lock.immersive.exception.BusinessException;
import com.lock.immersive.mapper.ProductItemMapper;
import com.lock.immersive.mapper.ProductMapper;
import com.lock.immersive.pojo.domain.Product;
import com.lock.immersive.pojo.domain.ProductItem;
import com.lock.immersive.pojo.dto.ProductCondition;
import com.lock.immersive.pojo.dto.ProductDto;
import com.lock.immersive.pojo.dto.ProductItemDto;
import com.lock.immersive.pojo.dto.SearchProductDto;
import com.lock.immersive.service.ProductService;
import com.lock.immersive.utils.ErrorCode;
import com.lock.immersive.utils.ResponseApi;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    public ProductService productService;

    @Resource
    public ProductItemMapper productItemMapper;
    @Resource
    public ProductMapper productMapper;

    @PostMapping("/insertProduct")
    @AuthCheck(mustRole = "superManager")
    public ResponseApi<Object> insertProduct(@RequestBody ProductDto productDto, HttpServletRequest request) {
        return productService.insertProduct(productDto, request);
    }


    @PostMapping("/selectProductByCondition")
    public ResponseApi<List<Product>> selectProductByCondition(@RequestBody SearchProductDto product){
        return ResponseApi.success(productMapper.selectProductByCondition(product));
    }
    @PostMapping("/insertProductItem")
    @AuthCheck(mustRole = "manager")
    public ResponseApi<Object> insertProductItem(@RequestBody ProductItemDto productItemDto) {
        return productService.insertProductItem(productItemDto);
    }

    @PostMapping("/getProductList")
    public Page<Product> getProductList(@RequestBody ProductCondition condition) {
        return productService.getProductList(condition);
    }

    @GetMapping("/getProductItemList")
    public ResponseApi<List<ProductItem>> getProductItemList(@RequestParam("productId") Integer productId) {
        return productService.getProductItemList(productId);
    }

    @PostMapping("/updateProduct")
    @AuthCheck(mustRole = "manager")
    public ResponseApi<Object> updateProduct(@RequestBody Product product) {
        return productService.updateProduct(product);
    }

    @PostMapping("/updateProductItem")
    @AuthCheck(mustRole = "manager")
    public ResponseApi<Object> updateProductItem(@RequestBody ProductItem productItem) {
        return productService.updateProductItem(productItem);
    }

    @PostMapping("/deleteProductItemList")
    @AuthCheck(mustRole = "superManager")
    public ResponseApi<Object> deleteProductItem(@RequestBody List<String> ids) {
        int i = productItemMapper.deleteBatchIds(ids);
        if (i > 0) {
            return ResponseApi.success("删除成功");
        } else {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
    }
    @PostMapping("/deleteProductList")
    @AuthCheck(mustRole = "superManager")
    public ResponseApi<Object> deleteProduct(@RequestBody List<String> ids) {
        return productService.deleteProductList(ids);
    }
}
